package com.zhu.ecology;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Antin_zhu on 2016/3/9.
 */
public class PostTest {

    private HttpClient httpClient;

    public void init(){
        if(httpClient == null) {
            httpClient =new HttpClient();

            List<Header> headers = new ArrayList<Header>();
            headers.add(new Header("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)"));
            httpClient.getHostConfiguration().getParams().setParameter("http.default-headers", headers);
        }
    }

    public String getResponseOfUrl(String url, Map<String, String> params){
        init();

        PostMethod signMethod = new PostMethod(url);
        for(Map.Entry<String, String> entry : params.entrySet()) {
            signMethod.addParameter(entry.getKey(), entry.getValue());
        }
        try {
            if (HttpStatus.SC_OK == httpClient.executeMethod(signMethod)) {
                return signMethod.getResponseBodyAsString();
            }
        }catch(IOException e){
            e.printStackTrace();
        }

        return "";
    }

    public static void main(String[] args){
        if(args == null || args.length%2 != 1){
            System.out.println("please input url with args");
            return;
        }

        String url = args[0];
        Map<String, String> params = new HashMap<String, String>();
        if(args.length > 1){
           for(int i = 1; i < args.length; i+= 2){
               params.put(args[i], args[i+1]);
           }
        }

        PostTest post= new PostTest();

        System.out.println(formatJson(post.getResponseOfUrl(url, params)));
    }

    /**
     * 格式化
     * @param jsonStr
     * @return
     * @author   lizhgb
     * @Date   2015-10-14 下午1:17:35
     */
    public static String formatJson(String jsonStr) {
        if (null == jsonStr || "".equals(jsonStr)) return "";
        StringBuilder sb = new StringBuilder();
        char last = '\0';
        char current = '\0';
        int indent = 0;
        for (int i = 0; i < jsonStr.length(); i++) {
            last = current;
            current = jsonStr.charAt(i);
            switch (current) {
                case '{':
                case '[':
                    sb.append(current);
                    sb.append('\n');
                    indent++;
                    addIndentBlank(sb, indent);
                    break;
                case '}':
                case ']':
                    sb.append('\n');
                    indent--;
                    addIndentBlank(sb, indent);
                    sb.append(current);
                    break;
                case ',':
                    sb.append(current);
                    if (last != '\\') {
                        sb.append('\n');
                        addIndentBlank(sb, indent);
                    }
                    break;
                default:
                    sb.append(current);
            }
        }

        return sb.toString();
    }

    /**
     * 添加space
     * @param sb
     * @param indent
     * @author   lizhgb
     * @Date   2015-10-14 上午10:38:04
     */
    private static void addIndentBlank(StringBuilder sb, int indent) {
        for (int i = 0; i < indent; i++) {
            sb.append('\t');
        }
    }
}
